import friends.*;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Random;

public class Main {

    public static void main(String[] args) {
        // create normal list of friends
        ArrayList<Friend> friends = new ArrayList<Friend>();

        // populate with values
        for (Friend friend: Friend.values())
            friends.add(friend);

        System.out.println(" -- original list --");
        for (Friend friend: friends)
            System.out.print(String.format("-> %s (%d) ", friend.name(), friend.age()));
        System.out.println();

        ArrayList<Friend> sorted = new ArrayList<Friend>(friends.size());
        for (int i = 0; i < friends.size(); i++)
            sorted.add(null);

        // variant 1 - by ordinal
        for (int i = 0; i < friends.size(); i++) {
            Friend friend = friends.get(i);
            sorted.set(friend.ordinal(), friend);
        }

        System.out.println(" -- sorting by ordinal --");
        for (Friend friend: sorted)
            System.out.print(String.format("-> %s (%d) ", friend.name(), friend.age()));
        System.out.println();

        // variant 2 - by value (age)
        Collections.sort(sorted, new Comparator<Friend>() {
            @Override
            public int compare(Friend f1, Friend f2) {
                // compareTo alternative for age
                return (f1.age() > f2.age()) 
                    ? 1 : (f1.age() < f2.age()) 
                        ? -1 : 0;
            }
        });

        System.out.println(" -- sorting by age --");
        for (Friend friend: sorted)
            System.out.print(String.format("-> %s (%d) ", friend.name(), friend.age()));
        System.out.println();

        // variant 3 - by name
        Collections.sort(sorted, new Comparator<Friend>() {
            @Override
            public int compare(Friend f1, Friend f2) {
                return f1.name().compareTo(f2.name());
            }
        });

        System.out.println(" -- sorting by name --");
        for (Friend friend: sorted)
            System.out.print(String.format("-> %s (%d) ", friend.name(), friend.age()));
        System.out.println();

        // variant 4 - swap 2 friends
        System.out.println(" -- swap 2 random friends --");

        // pick 2 random kids and print em
        Random random = new Random();
        int swap1 = random.nextInt(sorted.size());
        int swap2 = random.nextInt(sorted.size());
        System.out.println(String.format("swap %s (#%d) with %s (#%d)", sorted.get(swap1).name(), swap1 + 1, sorted.get(swap2).name(), swap2 + 1));

        Friend temp = sorted.get(swap1);
        sorted.set(swap1, sorted.get(swap2));
        sorted.set(swap2, temp);

        for (Friend friend: sorted)
            System.out.print(String.format("-> %s (%d) ", friend.name(), friend.age()));
   }
}
